<?php
defined('_JEXEC') or die( 'Restricted access' );
class ComponentLibs
{		
	function getItem()
	{
		$alias =ComponentLibs::getAliasName();			
		$componentLibs = new ComponentLibs();
		$queryalias = $componentLibs->getSql('itemid',$alias);
		$ralias=mysql_fetch_assoc($queryalias);
		return  $ralias['id'];
	}
	
	function getComponentData()
	{
		$alias =ComponentLibs::getAliasName();	
		$componentLibs = new ComponentLibs();
		$queryalias = $componentLibs->getSql('comdata',$alias);
		$ralias=mysql_fetch_assoc($queryalias);
		return  $ralias;
	}
	
	function getAliasName()
	{
		if(@$_GET['alias']){
			$alias = mysql_real_escape_string($_GET['alias']);
		}else{
			$alias = 'tin-tuc';
		}
		return $alias;
	}
	
	function getComponentImages($imagetext)
	{
		$img_url="";
		$find_img=strpos($imagetext,"img");
		if ($find_img){
		  $end_img=strpos($imagetext,"src=",$find_img);
		  if ($end_img){
			$j=$end_img;
			while (($imagetext[$j]!='"')||($j<count($imagetext))){
			  $img_url.=$imagetext[$j];
			  $j++;
			  if ($img_url=="src="){
				$img_url="";
				$j++;
			  }
			}
		  }
		}		
		return $img_url;
	}
		
	function applyMeta($title=NULL)
	{	
		require_once ( PATH_LIBRARIES.DS.'meta.php' );
		
		if(trim($_SESSION['title'])!='')
		{
			$title = $_SESSION['title'];
		}
		echo '<title>'.$title.'</title>';
		
		if(trim($_SESSION['description'])!='')
		{
			$description = $_SESSION['description'];
		}
		echo "<meta content='$description' name='description'>";
		
		if(trim($_SESSION['keywords'])!='')
		{
			$keywords = $_SESSION['keywords'];
		}
		echo "<meta content='$keywords' name='keywords'>";
		
		$metaData = ob_get_contents();
					ob_get_clean();
				
		return $metaData;				
	}
	
	function getModuleData($modulePosition)
	{			
		$ItemId =ComponentLibs::getItem();					
		$sql="select m.id,module,showtitle,title,params from dm_modules AS m, dm_modules_menu AS mm ";
		$sql.="where   m.id=mm.moduleid AND mm.menuid=$ItemId AND position='$modulePosition' AND published=1 order by ordering";
		$query=mysql_query($sql);	
			ob_start();
			while($row=mysql_fetch_array($query))
			{				
				$module = $row['module'];
				$moduleid = $row['id'];	
				$showtitle = $row['showtitle'];	
				$params = ComponentLibs::getParams($row['params']);				
				$Path = PATH_ROOT.DS."mods".DS.$module.DS."index.php";
				if (file_exists($Path)) {
					echo "<div class='module'>";
						if($showtitle ==1)
						{
							$title = $row['title'];	
							echo "<h3>$title</h3>";
						}
						
						include ( $Path );		
					echo "</div>";
				}				
			}
			$dataModule = ob_get_contents();
			ob_get_clean();		
			return $dataModule;
	}
	
	function applyComponent($component, $t)
	{
		$meta = ComponentLibs::applyMeta();
		
		
		
		$t = ComponentLibs::parttmp($t);				
		
		$t =  str_replace("{head}" , $meta, $t);
		$t= str_replace("{component}" , $component, $t);
		return $t;
	}
	
	function parttmp($t)
	{		
		$yummy = HamhaydungLibs::yummy_();
		$healthy = HamhaydungLibs::healthy();
		return strrev(str_replace($yummy, $healthy, $t));	
	}
	
	function tmpl($t)
	{	
		return $t;
	}
	
	function getParams($param)
	{			
		$paramarr = explode("\n",$param);
		$arr = array();
		foreach($paramarr AS $pr)
		{
			$prrr = explode("=",$pr);
			$arr[$prrr[0]] = @$prrr[1];		
		}
		return  $arr;
	}

	function execute($t)
	{
		$positionXml=PATH_LIBRARIES.DS.'tmp'.DS.'position.xml';
		$positionData = simplexml_load_file($positionXml);		
		foreach($positionData->position AS $position)
		{		
			$dataModule =	ComponentLibs::getModuleData($position);
			$t =   str_replace("{".$position."}" , $dataModule, $t);	
		}		
		ComponentLibs::removetmpl($t);			
	}
	
	function removetmpl($t)
	{			
		if(preg_match_all('#{([^"]+)}#iU', $t, $matches))
		{					
			$matches[0] = array_reverse($matches[0]);
			$count = count($matches[0]);			
			for($i = 0; $i < $count; $i++)
			{			
				$rmmatches  = $matches[0][$i];			
				$t = str_replace($rmmatches, '', $t);
			}			
		}
		echo $t;			
	}	
	
	function getComponent()
	{
		$data =ComponentLibs::getComponentData();		
		$component =$data['option'];
		$Componentparams = $data['params'];
		$ComponentName = $data['name'];
		$params = ComponentLibs::getParams($Componentparams);		
		ob_start();
		require_once ( PATH_LIBRARIES.DS.$component.DS.'index.php' );
		$component = 	ob_get_contents();
		ob_get_clean();
		return $component;
	}
	
	function breadcrumbs($string, $home='')
	{		
		$ttt='<?php ';
		if($home!='')
		{
			$ttt.=' $breadcrumb_home=1;';
		}
		$home='Trang chủ#$#'.BASE_URL;
		$ttt.=' $items[]="'.$home.'";';
		foreach($string AS $a)
		{
			$ttt.=' $items[]="'.$a.'";';
		}	
		$ttt.=' ?>';	
		$linkbr = PATH_LIBRARIES.'/breadcrumb.php';
		file_put_contents($linkbr, $ttt);
	}
	
	function getSql($type, $params = NULL)
	{		
		switch($type)
		{
			case $type=='position':
			$sql="select position from dm_modules where published=1 group by position";
			break;	
		
			case $type=='itemid':
			$sql="SELECT `id` FROM `dm_menu` AS m WHERE published=1 AND m.alias='$params' LIMIT 0,1";
			break;

			case $type=='comdata':
			 $sql="SELECT `option`, m.params, m.name FROM `dm_components` AS com, `dm_menu` AS m WHERE com.id=m.componentid AND m.alias='$params' AND m.published=1 LIMIT 0,1";
			break;				
		}
		$query=mysql_query($sql);		
		return $query;
	}
}


	